#!/usr/bin/env python
# coding: utf-8

"""
Created on 17/10/2011

@author: Rubén López
"""

import wx.aui
import wx.lib.scrolledpanel as scrolled

from editor import AlphabetEditorFrame, DefinitionEditorFrame
from page import InterpreterPage

ID_ALPHABET = wx.NewId()
ID_DEFINITION = wx.NewId()

class MainFrame(wx.Frame):
    """Main window of the application. It can handle several interpreter 
    pages, show the alphabet editor and the regular definition editor.
    """
    
    def __init__(self):
        wx.Frame.__init__(self, None, -1,
                                          title="Visual Lexical Analyzer",
                                          size=(640, 480),
                                          style=wx.DEFAULT_FRAME_STYLE)
        
        self.alphabetEditor = AlphabetEditorFrame()
        self.definitionEditor = DefinitionEditorFrame()
        self.CreateMenuBar()
        self.CreateStatusBar()
        
        # Here we create a panel and a notebook on the panel
        self.p = scrolled.ScrolledPanel(self)
        self.nb = wx.aui.AuiNotebook(self.p)
        self.count = 0
        
        # Bind event to clean temporary images of interpreter pages
        self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, 
          self.OnCloseInterpreter, self.nb)
        
        # Bind close evento to clean temporary images fo interpreter pages
        self.Bind(wx.EVT_CLOSE, self.OnExit, self)

        # put the notebook in a sizer for the panel to manage the layout
        sizer = wx.BoxSizer()
        sizer.Add(self.nb, 1, wx.EXPAND)
        self.p.SetSizer(sizer)
        self.p.SetupScrolling()

    def CreateMenuBar(self):
        """Creates the menu bar with one menu with the options to add an 
        interpreter page and open the alaphabet editor and the regular 
        definition editor.
        """
        
        menu = wx.Menu()
        for id_, label, helpText, handler in \
            [(wx.ID_OPEN, "&New Interpreter", 
              "Add a new interpreter page", self.OnNewInterpreter),
             (ID_ALPHABET, "&Alphabets Editor", 
              "Open the alphabets editor", self.OnAlphabetsEditor),
             (ID_DEFINITION, "&Definitions Editor", 
              "Open the definitions editor", self.OnDefinitionsEditor)]:
            if id_ == None:
                menu.AppendSeparator()
            else:
                item = menu.Append(id_, label, helpText)
                self.Bind(wx.EVT_MENU, handler, item)
        bar = wx.MenuBar()
        bar.Append(menu, "&File") 
        self.SetMenuBar(bar)
        
    def OnAlphabetsEditor(self, event):
        """Show the alphabet editor."""
        
        self.alphabetEditor.Show()
        
    def OnDefinitionsEditor(self, event):
        """Show the regular definition editor."""
        
        self.definitionEditor.Show()
        
    def OnNewInterpreter(self, evt):
        """Add a new child frame."""
        
        self.count += 1
        page = InterpreterPage(self, self.count)
        self.nb.AddPage(page, "Interpreter " + str(self.count))
        
    def OnCloseInterpreter(self, event):
        """Delete all temporary images for the closed interpreter."""
        
        index = event.GetSelection() 
        page = self.nb.GetPage(index)
        page.deleteImages()
    
    def OnExit(self, event):
        """Exit the application. Delete all temporary png files befor exit."""
        
        for i in range(self.nb.GetPageCount()):
            page = self.nb.GetPage(i)
            page.deleteImages(True)
        
        event.Skip()
        
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
